#include <STC15F2K60S2.H>
#include <ds1302.h>
void display();unsigned char i;
void Timer0Init(void);//1ms定时器0
void pro_date();//产生ds1302的数据
void read_time();
void produce_data();




 code unsigned char readaddre[5]={0x81,0x83,0x85};
 code unsigned char writeaddre[5]={0x80,0x82,0x84};
 code unsigned char tab[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf};//数码管0-9
 code unsigned char init_tab[]={0x59,0x59,0x12};
 unsigned char dspbuf[]={0xbf,0xbf,0xbf,0xbf,0xbf,0xbf,0xbf,0xbf,0xbf};
  unsigned char shi_temp;
  unsigned char fen_temp;
  unsigned char miao_temp;
 unsigned char shi;
 unsigned char fen;
 unsigned char miao;
 bit xun;
	 
unsigned char inter;
 
	 
	 
void main()
{
	set_sfm(23,59,50);
	Timer0Init();//定时器0
	while(1)
	 {
		 read_time();
		 produce_data();
	 	  if(xun)
        {
           xun=0;
           
	       }
   }
 }

 


void Timer0Init(void)		//1毫秒@12.000MHz
{
	AUXR |= 0x80;		//定时器时钟1T模式
	TMOD &= 0xF0;		//设置定时器模式
	TL0 = 0x20;		//设置定时初值
	TH0 = 0xD1;		//设置定时初值
	TF0 = 0;
  ET0=1;
	EA=1;//清除TF0标志
	TR0 = 1;		//定时器0开始计时
}


void time_irs_0(void) interrupt 1
{
	if(++inter==50)
   {
		 inter=0;
     xun=1;
   }
  display();
}








	
void display()
{

	//消影
	P2=((P2&0x1f)|0xef);
	P0=0xff;
	P2&=0x1f;
	
	//位选
	P2=((P2&0x1f)|0xcf);
	P0=1<<i;
	P2&=0x1f;
	
	//段选
	P2=((P2&0x1f)|0xef);
	P0=tab[dspbuf[i]];
	P2&=0x1f;
	
	if(++i==8)
	{
		i=0;
	}
}


void read_time()
{
	shi_temp=Read_Ds1302_Byte (0x85);
	fen_temp=Read_Ds1302_Byte (0x83);
	miao_temp=Read_Ds1302_Byte (0x81);
	
}

void produce_data()
{  
	shi=shi_temp/16*10+shi_temp%16;
	fen=fen_temp/16*10+fen_temp%16;
	miao=miao_temp/16*10+miao_temp%16;
	dspbuf[0]=shi/10;
	dspbuf[1]=shi%10;
dspbuf[2]=11;
	dspbuf[3]=fen/10;
	dspbuf[4]=fen%10;
	dspbuf[5]=11;
	dspbuf[6]=miao/10;
	dspbuf[7]=miao%10;
}





